//
//  AppUpdateViewController.m
//  ShangHaiHuDongApp
//
//  Created by ZJ on 2017/9/20.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "AppUpdateViewController.h"
#import "ZJAppUpdateAlertView.h"
#import "WelcomeView.h"
@interface AppUpdateViewController ()<ZJAppUpdateAlertViewDelegate , WelcomeViewDelegate>
{
    NSDictionary *_appUpdateInfo;
    JCAlertView * alertView;
}
/** imageView */
@property (nonatomic, strong) UIImageView *imageView;
@end

@implementation AppUpdateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.imageView];
    [self netWorking_appUpdate];
    
}

#pragma mark - 代理消失改页面
- (void)delegate_show{
//    //获取应用程序版本号
//    NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
//    NSString *currentVersion = [infoDict objectForKey:@"CFBundleShortVersionString"];
//    NSString *rememberVersion = @"0";
//    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"currentversion"]) {
//        rememberVersion = [[NSUserDefaults standardUserDefaults] objectForKey:@"currentversion"];
//    }
//    if ([currentVersion floatValue] > [rememberVersion floatValue]) {
//        WelcomeView * welcomeView = [[WelcomeView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], [UIScreen zj_screenHeight])];
//        welcomeView.delegate = self;
//        [self.view addSubview:welcomeView];
//    }
//    else{
        if (self.delegate) {
            [self.delegate AppUpdateViewControllerDeleGate_showWithIsWelcomeIn:NO];
        }
//    }
}

-(void)didClickEnterNext
{
    if (self.delegate) {
        [self.delegate AppUpdateViewControllerDeleGate_showWithIsWelcomeIn:YES];
    }
}

#pragma mark - 软件更新
- (void)netWorking_appUpdate{
    __weak typeof(self) weakSelf = self;
    [YBRequestManager request_versionWithSuccessBlock:^(id object) {
        if ([object[@"code"] integerValue] == 1) {
            NSArray * tempArray = object[@"data"];
            for (NSDictionary * tempDic in tempArray) {
                if ([tempDic[@"type"] integerValue] == 0) {
                    [weakSelf appUpdate_Dict:tempDic];
                }
            }
        }
        else{
            [weakSelf delegate_show];
        }
    } failBlock:^(id object) {
        [weakSelf delegate_show];
    }];
}




///版本对比
- (void)appUpdate_Dict:(NSDictionary *)dict{
    _appUpdateInfo = dict;
    //获取应用程序版本号
    NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
    NSString *currentVersion = [infoDict objectForKey:@"CFBundleShortVersionString"];
    //网上版本号
    NSString *newVersionCode = [dict objectForKey:@"version"];
    if ([newVersionCode floatValue] > [currentVersion floatValue]) {//更新
        ZJAppUpdateAlertView *view = [[ZJAppUpdateAlertView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        view.dict = dict;
        view.delegate = self;
        alertView = [[JCAlertView alloc] initWithCustomView:view dismissWhenTouchedBackground:YES];
        [alertView show];
    }else{//没有更新
        [self delegate_show];
    }
}

- (void)ZJAppUpdateAlertViewDelegate_canleClick{
    [alertView dismissWithCompletion:^{
        [self delegate_show];
    }];
}

- (void)ZJAppUpdateAlertViewDelegate_updateClick{
    
    /**  跳转更新应用  */
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/cn/app/中物院云课堂/id1453501536?mt=8"]];
    [[UIApplication sharedApplication] openURL:url];
    
}


- (UIImageView *)imageView{
    if (_imageView == nil){
        _imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
        _imageView.image = [UIImage imageNamed:@"welcomepage"];
    }
    return _imageView;
}

@end
